package makovo.ajax;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import makovo.data.Resources;
import makovo.entities.Applicant;
import makovo.entities.SysUser;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class ApplicantList extends ActionSupport implements ServletRequestAware, SessionAware, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest mRequest;
	private Map<String, Object> mSession;
	private ArrayList<Applicant> applicants;
	private String mNewLoc;
	private final int PAGE_SIZE = 50;
	private long mTotal = 0;

	public ArrayList<Applicant> getApplicants() {
		return this.applicants;
	}

	public String getNewLoc() {
		return this.mNewLoc;
	}

	public int getPageSize() {
		return PAGE_SIZE;
	}

	public long getTotal() {
		return mTotal;
	}

	public String showApplicants() {
		try {
			String curp;
			Integer state;
			Integer step;
			Integer city;
			Integer page = null;
			Integer change = 0;

			String par = this.mRequest.getParameter("id");
			curp = par.equals("") ? null : par;

			par = this.mRequest.getParameter("state");
			state = par.equals("-1") ? null : Integer.valueOf(par);

			par = this.mRequest.getParameter("change");
			change = Integer.valueOf(par);

			par = this.mRequest.getParameter("step");
			if (par.equals("-1"))
				step = null;
			else
				step = Integer.valueOf(par);

			par = this.mRequest.getParameter("city");
			city = par.equals("-1") ? null : Integer.valueOf(par);

			par = this.mRequest.getParameter("page");
			page = Integer.valueOf(par);

			String typeUser = (String) mSession.get("LogintipoUsr");

			if (typeUser != null)
				if (typeUser.equals("4")) {
					SysUser u = Resources.getUserByMail((String) mSession.get("login.email"));

					state = u.idState;
					city = u.idCity;
					// loc = u.idLoc;
				}

			if (change > 0)
				try {
					mTotal = Resources.getTotalApplicantsFilter(curp, step, state, city, null);
					mSession.put("Applicant.totalPage", mTotal);
				} catch (Exception e) {
					e.printStackTrace();
				}
			else
				mTotal = (long) mSession.get("Applicant.totalPage");
			
			applicants = Resources.getApplicants(curp, step, state, city, null, page, PAGE_SIZE);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ActionSupport.SUCCESS;
	}

	public String navigateContinue() {
		try {
			String id = this.mRequest.getParameter("id");
			this.mSession.put("User.idSender", id);

			Applicant a = Resources.getApplicantInfoByCURP(id);

			switch (a.step) {
			case 2:
				this.mNewLoc = "../paginas/info-socioeconomica.jsp";
				break;
			case 3:
				this.mNewLoc = "../paginas/proyecto.jsp";
				break;
			case 4:
				this.mNewLoc = "../tareas/documentos.jsp";
				break;
			case 5:
				this.mNewLoc = "../tareas/documentos_vivienda.jsp";
				break;
			default:
				this.mNewLoc = "../paginas/lista_aplicantes.jsp";
				break;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return ActionSupport.SUCCESS;
	}

	public String navigateDetails() {
		try {
			String id = this.mRequest.getParameter("id");
			this.mSession.put("User.idSender", id);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return ActionSupport.SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.mRequest = request;
	}

	@Override
	public void setSession(Map<String, Object> sessionInfo) {
		this.mSession = sessionInfo;
	}
}
